home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / src / dsearch / dsearch.c < prev   
C/C++ Source or Header  |  1997-09-09  |  3KB  |  170 lines

  1. /*
  2.  *    (c)Copyright 1992-1997 Obvious Implementations Corp.  Redistribution and
  3.  *    use is allowed under the terms of the DICE-LICENSE FILE,
  4.  *    DICE-LICENSE.TXT.
  5.  */
  6.  
  7. /*
  8.  *  DSEARCH.C
  9.  *
  10.  *  DSEARCH string files ...
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <string.h>
  16. #include <ctype.h>
  17. #include <lib/version.h>
  18.  
  19. #ifdef _DCC
  20. IDENT("DSEARCH", ".03");
  21. DCOPYRIGHT;
  22. #endif
  23.  
  24. static char Tmp[256];
  25. static long LastLine = -1;    /*  offset of base of last printed line */
  26.  
  27. short QuietOpt = 0;
  28.  
  29. void look(char *, char *, FILE *);
  30. void print_line(FILE *, long, char *, char *, char *);
  31.  
  32. main(ac, av)
  33. int   ac;
  34. char **av;
  35. {
  36.     int i;
  37.  
  38.     if (ac == 1) {
  39.     puts(Ident);
  40.     puts("DSEARCH srch-string files");
  41.     puts("can use AmigaDOS wildcards for files");
  42.     exit(1);
  43.     }
  44.     {
  45.     char *ptr;
  46.  
  47.     if ((ptr = strrchr(av[0], '/')) == NULL && (ptr = strrchr(av[0], ':')) == NULL)
  48.         ptr = av[0];
  49.     else
  50.         ++ptr;
  51.     }
  52.  
  53. #ifndef unix
  54.     expand_args(ac, av, &ac, &av);
  55. #endif
  56.  
  57.     for (i = 2; i < ac; ++i) {
  58.     FILE *fi;
  59.  
  60.     if (fi = fopen(av[i], "r")) {
  61.         if (ac > 2 && !QuietOpt)
  62.         printf("--- %s ---\n", av[i]);
  63.         LastLine = -1;
  64.         look(av[1], av[i], fi);
  65.         fclose(fi);
  66.     } else {
  67.         if (QuietOpt == 0)
  68.         printf ("--- %s --- (unable to open)\n", av[i]);
  69.     }
  70.     }
  71.     return(0);
  72. }
  73.  
  74. char Buf[8192];
  75.  
  76. void
  77. look(str, fileName, fi)
  78. char *str;
  79. char *fileName;
  80. FILE *fi;
  81. {
  82.     int len = strlen(str);
  83.     long off = 0;
  84.     short n;
  85.     short nn;
  86.     char *ptr;
  87.  
  88.     while ((nn = fread(Buf, 1, sizeof(Buf), fi)) > 0) {
  89.     n = nn;
  90.     for (ptr = Buf; n > 0; ++ptr, --n) {
  91.         if (((*str ^ *ptr) & ~0x20) == 0) {
  92.         if (n < len) {
  93.             if (strnicmp(ptr, str, n) == 0) {    /*  doesn't happen */
  94.             fseek(fi, off + nn - n, 0);    /*  often, I hope. */
  95.             break;
  96.             }
  97.         } else {
  98.             if (strnicmp(ptr, str, len) == 0) {
  99.             if (QuietOpt && LastLine == -1)
  100.                 printf("--- %s ---\n", fileName);
  101.  
  102.             print_line(fi, off, Buf, ptr, Buf + nn);
  103.             }
  104.         }
  105.         }
  106.     }
  107.     off = ftell(fi);
  108.     }
  109. }
  110.  
  111. void
  112. print_line(fi, off, base, ptr, endptr)
  113. FILE *fi;
  114. long off;
  115. char *base;
  116. char *ptr;
  117. char *endptr;
  118. {
  119.     long savpos = ftell(fi);
  120.     short sook = 0;            /*    nobody said I 'aught to use good 'nglish!   */
  121.  
  122.     /*
  123.      *    search backwards for newline
  124.      */
  125.  
  126.     while (*ptr != '\n') {
  127.     if (ptr <= base) {        /*    doesn't happen often */
  128.         if (off == 0)        /*    beginning of file */
  129.         break;
  130.         off -= sizeof(Tmp);
  131.         if (off < 0)
  132.         off = 0;
  133.         fseek(fi, off, 0);
  134.         sook = 1;
  135.         base = Tmp;
  136.         endptr = base + fread(Tmp, 1, sizeof(Tmp), fi);
  137.         ptr = endptr;
  138.     }
  139.     --ptr;
  140.     }
  141.     if (LastLine != off + (endptr - ptr)) {
  142.     LastLine = off + (endptr - ptr);
  143.  
  144.        /*
  145.     *  Search forwards for newline or EOF
  146.     */
  147.  
  148.     do {
  149.         ++ptr;
  150.         if (ptr >= endptr) {    /*    doesn't happen often */
  151.         off += endptr - base;
  152.         fseek(fi, off, 0);
  153.         sook = 1;
  154.         base = Tmp;
  155.         endptr = base + fread(Tmp, 1, sizeof(Tmp), fi);
  156.         ptr = Tmp;
  157.         if (endptr <= base) {
  158.             putchar('\n');
  159.             break;
  160.         }
  161.         }
  162.         putchar(*ptr);
  163.     } while (*ptr != '\n');
  164.     }
  165.  
  166.     if (sook)
  167.     fseek(fi, savpos, 0);
  168. }
  169.  
  170.